<template>
  <div class="review-container">
    <!-- 评论列表 -->
	<button class="back-btn" @click="goBack">← 返回上一页</button>
    <!-- 发表评论 -->
    <div class="post-review">
      <h2 class="title">发表评论</h2>
      <div class="input-area">
        <textarea
          v-model="newReview"
          placeholder="请输入您的评论（不少于10个字）"
          class="review-input"
          maxlength="200"
        ></textarea>
        <div class="action">
          <span class="word-count">{{ newReview.length }}/200</span>
          <button 
            class="submit-btn"
            :disabled="newReview.length < 10"
            @click="handleSubmit"
          >
            提交评论
          </button>
        </div>
      </div>
    </div>

  </div>
  
</template>

<script>
import axios from 'axios';


export default {
  name: 'FoodReview',
  data() {
    return {
      reviews: [],      // 评论列表
      newReview: '',    // 新评论
      user: {},         // 用户信息
      foodId: null      // 食物ID
    };
  },
  created() {
    this.foodId = this.$route.query.foodId;
	this.user = this.$getSessionStorage('user') || {}; // 确保 this.user 不为空
    this.checkLogin();
    this.loadReviews();
  },
  methods: {
    // 登录验证（保持不变）
    checkLogin() { 
      /* 登录验证的逻辑 */
    },goBack() {
    this.$router.go(-1) // 路由回退
  },

    // 加载评论
    loadReviews() {
      this.$axios.get('/Review/ReviewListOfFood', {
        params: { foodId: this.foodId }
      })
      .then((res) => {
        if (res.data.result) { // 使用 result 判断是否成功
          this.reviews = res.data.result;
        } else {
          this.$message.error('评论加载失败');
        }
      })
      .catch((error) => {
        this.$message.error('评论加载失败');
        console.error('加载评论失败:', error);
      });
    },

    // 提交评论
    handleSubmit() {
      if (this.newReview.trim().length < 10) {
        return this.$message.warning('评论至少需要10个字');
      }

      this.$axios.post('/Review/ReviewId', this.$qs.stringify({
        foodId: this.foodId,
        userId: this.user.userId,
        content: this.newReview
      }))
      .then(res => {
        if (res.data.result) { // 使用 result 判断是否成功
          this.newReview = ''; // 清空评论输入框
          this.loadReviews(); // 重新加载评论列表
        } else {
          this.$message.error(res.data.message || '提交失败');
        }
      })
      .catch(error => {
        this.$message.error('提交出错，请稍后重试');
        console.error('提交错误:', error);
      });
    },

    // 删除评论
   

  },
  
};
</script>

<style scoped>
	.back-btn {
	  padding: 8px 16px;
	  margin-bottom: 20px;
	  background: #f5f5f5;
	  color: #666;
	  border: 1px solid #ddd;
	  border-radius: 4px;
	  cursor: pointer;
	  transition: all 0.3s;
	}
	
	.back-btn:hover {
	  background: #2d8cf0;
	  color: white;
	  border-color: #2d8cf0;
	  transform: translateY(-1px);
	  box-shadow: 0 2px 8px rgba(45, 140, 240, 0.2);
	}
.review-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 30px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.08);
}

.title {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #eee;
}

/* 发表评论 */
.post-review {
  margin-top: 40px;
}

.input-area {
  position: relative;
}

.review-input {
  width: 100%;
  height: 100px;
  padding: 15px;
  border: 2px solid #e8e8e8;
  border-radius: 8px;
  resize: none;
  font-size: 14px;
  transition: border-color 0.3s;

  &:focus {
    border-color: #2d8cf0;
    outline: none;
  }
}

.action {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;

  .word-count {
    font-size: 12px;
    color: #999;
  }
}

.submit-btn {
  padding: 8px 25px;
  background: #2d8cf0;
  color: #fff;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s;

  &:disabled {
    background: #c5c8ce;
    cursor: not-allowed;
  }

  &:not(:disabled):hover {
    opacity: 0.9;
    transform: translateY(-1px);
  }
}

.empty-tip {
  text-align: center;
  color: #999;
  padding: 40px 0;
}
</style>
